# tag::HACKERCLUB[]
from dataclasses import dataclass
from typing import ClassVar
from club import ClubMember

@dataclass
class HackerClubMember(ClubMember):  # <1>
    # all_handles = set()  # 注意这里没有冒号，就是一个静态变量！
    all_handles: ClassVar[set[str]] = set()  # 更好的写法：显式标注静态变量的类型！
    handle: str = ''  # <3>

    def __post_init__(self):
        cls = self.__class__  # cls就是HackerClubMember，意义何在？
        if self.handle == '':  # <5>
            self.handle = self.name.split()[0]
        if self.handle in cls.all_handles:  # <6>
            msg = f'handle {self.handle!r} already exists.'
            raise ValueError(msg)
        cls.all_handles.add(self.handle)  # <7>


# works as intended, but is not satisfactory to a static type checker. Next,
# we’ll see why, and how to fix it.
h1 = HackerClubMember('feng ji')
h2 = HackerClubMember('Scene MC')
# h3 = HackerClubMember(123123)
print(id(h1.all_handles) == id(h2.all_handles))
# end::HACKERCLUB[]
